//
//  MYGCaseFilesDetailHeaderView.swift
//  MYGrow
//
//  Created by 王振明 on 2024/3/16.
//

import UIKit
import QMUIKit
import JXSegmentedView

class MYGCaseFilesDetailHeaderView: UIView {
    var beforeBloodModel:MYGHealthStastiscSugarModel = MYGHealthStastiscSugarModel()
    var afterBloodModel:MYGHealthStastiscSugarModel = MYGHealthStastiscSugarModel()
    var bloodPresure:MYGHealthStastiscPressureModel = MYGHealthStastiscPressureModel()
    var heartValueModel:MYGHealthStastiscHeartModel = MYGHealthStastiscHeartModel()
//    var slectDate:((Int)->())?
//    
//    var titles:[String]?{
//        didSet{
//            configUI()
//            requestData()
//        }
//        
//    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
        requestData()
    }
    func requestData(){
        MMYGNetwork<MYGCaseFileNet, MYGPageModel<MYGHealthStastiscHeartModel>>.request(target: .getHeartInfo(params: ["patient_id":"0"])) {[weak self] result in
            guard let result = result else { return }
            guard let self = self else { return }
            if result.list.count > 0{
                self.heartValueModel = result.list[0]
            }
            refreshDataUI()
        } failed: { _, error in
        }
        MMYGNetwork<MYGCaseFileNet, MYGPageModel<MYGHealthStastiscPressureModel>>.request(target: .getBloodPresure(params: ["patient_id":"0"])) {[weak self] result in
            guard let result = result else { return }
            guard let self = self else { return }
            if result.list.count > 0{
                self.bloodPresure = result.list[0]
            }
            refreshDataUI()
        } failed: { _, error in
        }
        MMYGNetwork<MYGCaseFileNet, MYGPageModel<MYGHealthStastiscSugarModel>>.request(target: .getBloodList(params: ["type":"1","patient_id":"0"])) {[weak self] result in
            guard let result = result else { return }
            guard let self = self else { return }
            if result.list.count > 0{
                self.beforeBloodModel = result.list[0]
            }
            refreshDataUI()
        } failed: { _, error in
        }
        MMYGNetwork<MYGCaseFileNet, MYGPageModel<MYGHealthStastiscSugarModel>>.request(target: .getBloodList(params: ["type":"2","patient_id":"0"])) {[weak self] result in
            guard let result = result else { return }
            guard let self = self else { return }
            if result.list.count > 0{
                self.afterBloodModel = result.list[0]
            }
            refreshDataUI()
        } failed: { _, error in
        }
    }
    func refreshDataUI(){
        rightTopLabel1.text = beforeBloodModel.hba1c_value
        rightBottomLabel1.text = afterBloodModel.hba1c_value
        rightTopLabel.text = "\(bloodPresure.diastolic_pressure)"
        rightBottomLabel.text = "\(bloodPresure.systolic_pressure)"
        heartLabel.rz.colorfulConfer {[weak self] confer in
            confer.text("心率：")?.font(.medium(12)).textColor(.rgba(139, 139, 139, 1))
            confer.text("\(String(format: "%d", self?.heartValueModel.heart_rate_value ?? 0)) 次/分")?.font(.medium(18)).textColor(.rgba(0, 0, 0, 1))
        }
        
    }
    func configUI(){
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(20))
            make.top.equalToSuperview().offset(SCALEWIDTH(10))
            make.height.equalTo(SCALEWIDTH(33.5))
        }
        addSubview(shareBtn)
        shareBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-SCALEWIDTH(15))
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: SCALEWIDTH(23), height: SCALEWIDTH(20.5)))
        }
        
        addSubview(infoLabel)
        infoLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(20))
            make.top.equalTo(nameLabel.snp.bottom).offset(SCALEWIDTH(7.5))
            make.height.equalTo(SCALEWIDTH(22.5))
        }
        addSubview(hosImageV)
        hosImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(20))
            make.top.equalTo(infoLabel.snp.bottom).offset(SCALEWIDTH(12))
            make.size.equalTo(CGSize(width: SCALEWIDTH(20.5), height: SCALEWIDTH(16)))
        }
        addSubview(hospitalNameLabel)
        hospitalNameLabel.snp.makeConstraints { make in
            make.left.equalTo(hosImageV.snp.right).offset(SCALEWIDTH(2))
            make.centerY.equalTo(hosImageV)
            make.height.equalTo(SCALEWIDTH(20))
        }
        addSubview(dataView)
        dataView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(15))
            make.top.equalTo(hospitalNameLabel.snp.bottom).offset(SCALEWIDTH(16.5))
            make.right.equalToSuperview().offset(-SCALEWIDTH(15))
            make.height.equalTo(SCALEWIDTH(195.5))
        }
        
//        addSubview(redView)
//        addSubview(timeButton)
//        addSubview(searchButton)
//        timeButton.snp.makeConstraints { make in
//            make.top.equalTo(dataView.snp.bottom).offset(15)
//            make.left.equalTo(29)
//        }
//        redView.snp.makeConstraints { make in
//            make.left.equalTo(15)
//            make.width.height.equalTo(7)
//            make.centerY.equalTo(timeButton)
//        }
//        searchButton.snp.makeConstraints { make in
//            make.right.equalToSuperview().offset(-15)
//            make.centerY.equalTo(timeButton)
//        }
        
//        addSubview(segmentView)
//        segmentView.snp.makeConstraints { make in
//            make.left.equalTo(0)
//            make.top.equalTo(timeButton.snp.bottom).offset(15)
//            make.right.equalToSuperview()
//            make.height.equalTo(35)
//        }
        
        dataView.addSubview(dataImage)
        dataView.addSubview(tipLabel)
        dataView.addSubview(heartLabel)
        dataView.addSubview(blodLabel)
        dataView.addSubview(line)
        dataImage.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.height.equalTo(35)
            make.width.equalTo(116)
        }
        tipLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(2)
            make.height.equalTo(22)
        }
        heartLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.top.equalTo(dataImage.snp.bottom).offset(15)
            make.height.equalTo(25)
            make.right.equalTo(screenWidth/2)
        }
        blodLabel.snp.makeConstraints { make in
            make.left.equalTo(screenWidth/2)
            make.height.equalTo(25)
            make.centerY.equalTo(heartLabel)
        }
        
        line.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(1)
            make.top.equalTo(heartLabel.snp.bottom).offset(15)
        }
        
        
        dataView.addSubview(topLabel)
        dataView.addSubview(bgView)
        bgView.addSubview(leftTopLabel)
        bgView.addSubview(leftBottomLabel)
        bgView.addSubview(rightTopLabel)
        bgView.addSubview(rightBottomLabel)
        
        topLabel.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(15)
            make.height.equalTo(16)
            make.left.equalTo(heartLabel)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(heartLabel)
            make.top.equalTo(topLabel.snp.bottom).offset(7)
            make.height.equalTo(53)
            make.width.equalTo(121)
        }
        
        leftTopLabel.snp.makeConstraints { make in
            make.left.equalTo(7.5)
            make.top.equalTo(7.5)
            make.height.equalTo(16)
        }
        
        leftBottomLabel.snp.makeConstraints { make in
            make.left.equalTo(7.5)
            make.top.equalTo(leftTopLabel.snp.bottom).offset(5)
            make.height.equalTo(16)
        }
        
        rightTopLabel.snp.makeConstraints { make in
            make.centerY.equalTo(leftTopLabel)
            make.right.equalToSuperview().offset(-7.5)
        }
        
        rightBottomLabel.snp.makeConstraints { make in
            make.centerY.equalTo(leftBottomLabel)
            make.right.equalToSuperview().offset(-7.5)
        }
        dataView.addSubview(topLabel1)
        dataView.addSubview(bgView1)
        bgView1.addSubview(leftTopLabel1)
        bgView1.addSubview(leftBottomLabel1)
        bgView1.addSubview(rightTopLabel1)
        bgView1.addSubview(rightBottomLabel1)
        
        topLabel1.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(15)
            make.height.equalTo(16)
            make.left.equalTo(bgView.snp.right).offset(50)
        }
        
        bgView1.snp.makeConstraints { make in
            make.left.equalTo(bgView.snp.right).offset(50)
            make.top.equalTo(topLabel.snp.bottom).offset(7)
            make.height.equalTo(53)
            make.width.equalTo(121)
        }
        
        leftTopLabel1.snp.makeConstraints { make in
            make.left.equalTo(7.5)
            make.top.equalTo(7.5)
            make.height.equalTo(16)
        }
        
        leftBottomLabel1.snp.makeConstraints { make in
            make.left.equalTo(7.5)
            make.top.equalTo(leftTopLabel.snp.bottom).offset(5)
            make.height.equalTo(16)
        }
        
        rightTopLabel1.snp.makeConstraints { make in
            make.centerY.equalTo(leftTopLabel)
            make.right.equalToSuperview().offset(-7.5)
        }
        
        rightBottomLabel1.snp.makeConstraints { make in
            make.centerY.equalTo(leftBottomLabel)
            make.right.equalToSuperview().offset(-7.5)
        }
    }
    private lazy var nameLabel:UILabel = {
        let cont = UILabel()
        cont.font = .medium(24)
        cont.textColor = .rgba(51, 51, 51,1)
        cont.text =  "名字"
        return cont
    }()
    private lazy var infoLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(16)
        cont.textColor = .rgba(51, 51, 51,1)
        cont.text = "男丨75岁丨185cm"
        return cont
    }()
    private lazy var shareBtn:UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "file_share"), for: .normal)
        return cont
    }()
    private lazy var hosImageV:UIImageView = {
        let cont = UIImageView()
        cont.image = UIImage(named: "file_send")
        return cont
    }()
    private lazy var hospitalNameLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(14)
        cont.textColor = .rgba(121, 121, 121,1)
        cont.text = "安贞医院安贞医院安贞医院安贞医院"
        return cont
    }()
    var dataView:UIView = {
        let cont = UIView()
        cont.backgroundColor = .white
        cont.layerCornerRadius = SCALEWIDTH(6)
        cont.layer.shadowColor = RGBA(r: 0, g: 0, b: 0, a: 0.1).cgColor
        cont.layer.shadowOffset = CGSize(width: 0, height: 1)
        cont.layer.shadowRadius = 13
        cont.layer.shadowOpacity = 1
        cont.layer.masksToBounds = false
        return cont
    }()
    
    
    private lazy var dataImage: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "file_n")
        return imgV
    }()
    
    private lazy var tipLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(16)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "自测数据"
        return cont
    }()
    
    
    private lazy var heartLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(16)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.rz.colorfulConfer { confer in
            confer.text("心率：")?.font(.medium(12)).textColor(.rgba(139, 139, 139, 1))
            confer.text("79 次/分")?.font(.medium(18)).textColor(.rgba(0, 0, 0, 1))
        }
        
        return cont
    }()
    
    private lazy var blodLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(16)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.rz.colorfulConfer { confer in
            confer.text("血氧：")?.font(.medium(12)).textColor(.rgba(139, 139, 139, 1))
            confer.text("79 %")?.font(.medium(18)).textColor(.rgba(0, 0, 0, 1))
        }
        return cont
    }()
    
    private lazy var line:UIView = {
        let line = UIView()
        line.backgroundColor = .rgba(238, 238, 238, 1)
        return line
    }()
    
    private lazy var selectDateView: UIView = {
        let bgView = UIView()
        return bgView
    }()
    
    private lazy var redView: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "file_red")
        return imgV
    }()
    
    private lazy var searchButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "file_search"), for: .normal)
        return btn
    }()

    private lazy var timeButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Community/dropdown"), for: .normal)
        btn.spacingBetweenImageAndTitle = 4
        btn.imagePosition = .right
        btn.setTitle("2024年(07)", for: .normal)
        btn.setTitleColor(.rgba(51, 51, 51, 1), for: .normal)
        btn.titleLabel?.font = .medium(17)
        return btn
    }()
    
    private lazy var topLabel:UILabel = {
        let cont = UILabel()
        cont.font = .regular(12)
        cont.textColor = .rgba(139, 139, 139, 1)
        cont.text = "血压(mmhg)"
        return cont
    }()
    
    private lazy  var bgView: UIView = {
        let bgView = UIView()
        bgView.backgroundColor = .rgba(247, 247, 247, 1)
        return bgView
    }()
    
    private lazy var leftTopLabel:UILabel = {
        let cont = UILabel()
        cont.font = .medium(12)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "低压："
        return cont
    }()
    
    private lazy var leftBottomLabel:UILabel = {
        let cont = UILabel()
        cont.font = .medium(12)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "高压："
        return cont
    }()
    
    private lazy var rightTopLabel:UILabel = {
        let cont = UILabel()
        cont.font = .medium(12)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "70"
        return cont
    }()
    
    private lazy var rightBottomLabel:UILabel = {
        let cont = UILabel()
        cont.font = .medium(12)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "120"
        return cont
    }()
    
    //====
    private lazy var topLabel1:UILabel = {
        let cont = UILabel()
        cont.font = .regular(12)
        cont.textColor = .rgba(139, 139, 139, 1)
        cont.text = "血糖(mmhg)"
        return cont
    }()
    
    private lazy  var bgView1: UIView = {
        let bgView = UIView()
        bgView.backgroundColor = .rgba(247, 247, 247, 1)
        return bgView
    }()
    
    private lazy var leftTopLabel1:UILabel = {
        let cont = UILabel()
        cont.font = .medium(12)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "空腹："
        return cont
    }()
    
    private lazy var leftBottomLabel1:UILabel = {
        let cont = UILabel()
        cont.font = .medium(12)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "餐后："
        return cont
    }()
    
    private lazy var rightTopLabel1:UILabel = {
        let cont = UILabel()
        cont.font = .medium(12)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "70"
        return cont
    }()
    
    private lazy var rightBottomLabel1:UILabel = {
        let cont = UILabel()
        cont.font = .medium(12)
        cont.textColor = .rgba(51, 51, 51, 1)
        cont.text = "120"
        return cont
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    private lazy var segmentView: JXSegmentedView = {
//        let segment = JXSegmentedView()
//        segment.delegate = self
//        segment.dataSource = dataSource
//        segment.backgroundColor = .white
//        let indicator = JXSegmentedIndicatorLineView()
//        indicator.backgroundColor = .white
//        indicator.indicatorHeight = 0
//        indicator.indicatorWidth = 0
//        indicator.indicatorPosition = .bottom
//        
//        segment.indicators = [indicator]
//        return segment
//    }()
//    
//    private lazy var dataSource: JXSegmentedTitleDataSource = {
//        let dataSource = JXSegmentedTitleDataSource()
//        dataSource.titles = titles!
//        dataSource.titleNormalFont = .medium(14)
//        dataSource.titleNormalColor = .rgba(116, 117, 118, 1)
//        dataSource.titleSelectedFont = .regular(14)
//        dataSource.titleSelectedColor = .rgba(143, 134, 255, 1)
//
//        return dataSource
//    }()
 
    
    
}

//extension MYGCaseFilesDetailHeaderView:JXSegmentedViewDelegate{
//    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int){
//        slectDate?(index)
//    }
//}

